using Electric.Core;
using Electric.Domain.Entitys.Todos;
using Electric.Domain.Repository.Identity;
using Electric.Domain.Repository.Todo;
using Electric.Domain.Shared.Entitys.Todo;
using Electric.Domain.Specifications;
using Electric.Domain.Specifications.Todos;

namespace Electric.Domain.Manager.Todo;

public class TodoManager : IDomainService
{
    private IUserRepository _userRepository;
    private ITodoRepository _todoRepository;

    public bool AutoSaveChanges { get; set; } = true;

    public TodoManager(IUserRepository userRepository, ITodoRepository todoRepository)
    {
        _userRepository = userRepository;
        _todoRepository = todoRepository;
    }

    public async Task<EleTodo> CreateAsync(EleTodo todo)
    {
        EleTodo eleTodo = await _todoRepository.InsertAsync(todo, AutoSaveChanges);
        return eleTodo;
    }

    public async Task<List<EleTodo>> GetListAsync(string? name, string? remark, TodoStatus? status, int page, int prePage)
    {
        //搜索条件
        var specification = new TodoNameRemarkStatusFiltereSpecification(name, remark, status);

        //返回用户列表
        var skipCount = (page <= 0 ? 0 : page - 1) * prePage;

        System.Console.WriteLine(skipCount);

        var todos = await _todoRepository.GetListAsync(
            specification,
            skipCount,
            prePage,
            includeDetails: true,
            sorting: nameof(EleTodo.CreationTime) + " desc"
        );

        return todos;
    }

    public async Task<long> GetCountAsync(string? name, string? remark, TodoStatus? status)
    {
        //搜索条件
        var specification = new TodoNameRemarkStatusFiltereSpecification(name, remark, status);

        //返回用户列表的总记录数
        var total = await _todoRepository.GetCountAsync(specification);
        return total;
    }

    public async Task<EleTodo> FindByIdAsync(Guid id)
    {
        EleTodo eleTodo = await _todoRepository.FindAsync(id);
        return eleTodo;
    }

    public async Task DeleteAsync(Guid id)
    {
        await _todoRepository.DeleteAsync(id,AutoSaveChanges);
    }

    //?????????????:这里应该返回什么
    public async Task<EleTodo> UpdateAsync(EleTodo eleTodo)
    {
        EleTodo eleTodoUpdate = await _todoRepository.UpdateAsync(eleTodo,AutoSaveChanges);
        return eleTodoUpdate;
    }
}
